/* File makes Table 5 and table A.5 */

/* 	Input files: 	fw_info_flow_m, fw_info_flow
	Output files: T5_Feed_impressions.tex Feed_messages.tex */

**********************************************
clear all
macro drop _all
scalar drive		= "E:"
scalar maindir		= "`=drive'\Replicate"
scalar tabledir		= "`=drive'\Replicate\TablesRep"
cd "`=maindir'"
**********************************************


foreach filefile in    fw_info_flow_m fw_info_flow {  

use `filefile'

duplicates report userid symbolid date 
assert r(N) == r(unique_value)

foreach var of varlist _all {
	qui replace `var'=0 if `var'==.
}


foreach xx in bull bear {
gen sum_`xx'_2_10 = sum_`xx'_2 + sum_`xx'_3 + sum_`xx'_4 + sum_`xx'_5 + sum_`xx'_6 + sum_`xx'_7 + sum_`xx'_8 + sum_`xx'_9 + sum_`xx'_10
gen sum_`xx'_11_30 = sum_`xx'_11_20 + sum_`xx'_21_30 
gen sum_`xx'_31_50 = sum_`xx'_31_40 + sum_`xx'_41_50 
}


foreach i in 1 2_10 11_30 31_50 {  
	gen s`i'= sum_bull_`i' + sum_bear_`i'
	replace sum_bull_`i'=. if s`i'==0
	replace sum_bear_`i'=. if s`i'==0 
drop s`i'	
}

keep userid symbolid date bull bear sum_bull_1 sum_bear_1 sum_bull_2_10 sum_bear_2_10 sum_bull_11_30 sum_bear_11_30 sum_bull_31_50 sum_bear_31_50

qui compress
save temp_msgs, replace

**********************************************

foreach kk in 1 2_10 11_30 31_50 {
	
eststo N_t`kk'_bull: reghdfe sum_bull_`kk' bull, absorb(i.userid i.symbolid#i.date) vce(cl userid) 
	qui sum sum_bull_`kk' if e(sample)==1
	qui estadd scalar avg = r(mean)
	scalar define avg= r(mean)
	qui estadd scalar effect_size = (_b[bull]/avg)*100

eststo N_t`kk'_bear: reghdfe sum_bear_`kk' bear, absorb(i.userid i.symbolid#i.date) vce(cl userid) 
	qui sum sum_bear_`kk' if  e(sample)==1
	qui estadd scalar avg = r(mean)
	scalar define avg= r(mean)
	qui estadd scalar effect_size = (_b[bear]/avg)*100
}


********************************************


cap estadd local userfe "Y" : *
cap estadd local daysymbolfe "Y" : *

local stats_opts " stats(N N_clust r2 avg effect_size userfe daysymbolfe, fmt(%18.0fc %18.0fc %12.2fc %12.2fc %12.0fc) labels("\# obs." "\# clusters (users)" "$\textit{R}^2$" "Mean of dependent var.(\%)" "Effect size (\% of mean)" "User FE" "Day x symbol FE") ) " 

cd "`=tabledir'"

if "`filefile'" == "fw_info_flow" {  
	#delimit ; 
	local opts   "b(%16.2fc) se(%16.2fc) brackets booktabs  star(* 0.10 ** 0.05 *** 0.01) nonotes lines 
	mtitles("Bullish" "Bearish" "Bullish" "Bearish" "Bullish" "Bearish" "Bullish" "Bearish" "Bullish" "Bearish")
	mgroups("N impressions\textsubscript{i,s,t+1}" "N impressions\textsubscript{i,s,(t+2 $\rightarrow$ t+10)}" "N impressions\textsubscript{i,s,(t+11 $\rightarrow$ t+30)}" "N impressions\textsubscript{i,s,(t+31 $\rightarrow$ t+50)}" 
	, pattern(1 0 1 0 1 0 1 0 1 0) prefix(\multicolumn{@span}{c}{) suffix(}) span erepeat(\cmidrule(lr){@span}))  " ;     
	#delimit cr 

	esttab N* using T5_Feed_impressions.tex, `opts' `stats_opts' drop(_cons) coef(bull "$\mathbbm{1}$ Declare Bull\textsubscript{\textbf{i,s,t}}" bear "$\mathbbm{1}$ Declare Bear\textsubscript{\textbf{i,s,t}}") replace  
}

if "`filefile'" == "fw_info_flow_m" {  
	#delimit ; 
	local opts   "b(%16.2fc) se(%16.2fc) brackets booktabs  star(* 0.10 ** 0.05 *** 0.01) nonotes lines 
	mtitles("Bullish" "Bearish" "Bullish" "Bearish" "Bullish" "Bearish" "Bullish" "Bearish" "Bullish" "Bearish")
	mgroups("N messages\textsubscript{i,s,t+1}" "N messages\textsubscript{i,s,(t+2 $\rightarrow$ t+10)}" "N messages \textsubscript{i,s,(t+11 $\rightarrow$ t+30)}" "N messages\textsubscript{i,s,(t+31 $\rightarrow$ t+50)}" 
	, pattern(1 0 1 0 1 0 1 0 1 0) prefix(\multicolumn{@span}{c}{) suffix(}) span erepeat(\cmidrule(lr){@span}))  " ;     
	#delimit cr 

	esttab N* using Feed_messages.tex, `opts' `stats_opts' drop(_cons) coef(bull "$\mathbbm{1}$ Declare Bull\textsubscript{i,s,t}" bear "$\mathbbm{1}$ Declare Bear\textsubscript{i,s,t}") replace  
}
cd "`=maindir'"

}




cap n erase temp_msgs.dta



